#import "UtilsPlugin.h"
#import <IOSUtilsLibrary/HDLDeviceUtils.h>
#import <Reachability/Reachability.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@implementation UtilsPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"com.flutter.hlibrary/utils"
            binaryMessenger:[registrar messenger]];
  UtilsPlugin* instance = [[UtilsPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if([PHONE_TYPE isEqualToString:call.method]){
      // 判断当前运行的设备
      result([[NSNumber alloc]  initWithInt:[HDLDeviceUtils getPhoneType]]);
  } else if([LANGUAGE isEqualToString:call.method]){
      // 当前系统语言
      result([HDLDeviceUtils getLanguage]);
  } else if([CURRENT_NETWORK_TYPE isEqualToString:call.method]){
      // 获得当前网络
      result([self getNetconnType]);
  } else if([SCREEN_WIDTH isEqualToString:call.method]) {
      result([self getScreenWidth]);
  } else if([SCREEN_HEIGHT isEqualToString:call.method]) {
      result([self getScreenHeight]);
  } else if([SCREEN_RATIO isEqualToString:call.method]) {
      result([self getScreenRatio]);
  } else {
      result(FlutterMethodNotImplemented);
  }
}

/// 获取当前的网络状态2G，3G，wifi，或者4G
- (NSString *)getNetconnType{
    NSString *netconnType = @"";
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:// 没有网络
        {
        netconnType = @"no network";
        }
            break;
        case ReachableViaWiFi:// Wifi
        {
        netconnType = @"Wifi";
        }
            break;
        case ReachableViaWWAN:// 手机自带网络
        {
        // 获取手机网络类型
        CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
        NSString *currentStatus = info.currentRadioAccessTechnology;
        if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
            netconnType = @"GPRS";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
            netconnType = @"2.75G EDGE";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
            netconnType = @"3.5G HSDPA";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
            netconnType = @"3.5G HSUPA";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
            netconnType = @"2G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
            netconnType = @"3G";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
            netconnType = @"HRPD";
        }else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
            netconnType = @"4G";
        }
        }
            break;
        default:
            break;
    }
    return netconnType;
}

-(NSNumber*) getScreenWidth {
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    return [[NSNumber alloc] initWithFloat:size.width*scale_screen];
}

-(NSNumber*) getScreenHeight {
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    return [[NSNumber alloc] initWithFloat:size.height*scale_screen];
}

-(NSNumber*) getScreenRatio {
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    return [[NSNumber alloc] initWithFloat:scale_screen];
}


@end
